import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/postJson")
public class PostJsonServlet extends HttpServlet {

    // 创建 Jackson的核心对象
    ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 读取body的请求
        //                              参数1：要对哪里进行转换   参数二：要转换成什么类型的
        User user = objectMapper.readValue(req.getInputStream(), User.class);// 返回值就是一个User对象
        resp.setContentType("text/html; charset=utf8");
        resp.getWriter().write("aa = " + user.aa + ", bb = " + user.bb);
    }
}
class User{
    public int aa;
    public String bb;
}
